import _ from "lodash";
import { loginByToken } from "@/lib/Util";
export const forEach = (arr, fn) => {
  if (!arr.length || !fn) return;
  let i = -1;
  let len = arr.length;
  while (++i < len) {
    let item = arr[i];
    fn(item, i, arr);
  }
};

/**
 * @param {Array} arr1
 * @param {Array} arr2
 * @description 得到两个数组的交集, 两个数组的元素为数值或字符串
 */
export const getIntersection = (arr1, arr2) => {
  let len = Math.min(arr1.length, arr2.length);
  let i = -1;
  let res = [];
  while (++i < len) {
    const item = arr2[i];
    if (arr1.indexOf(item) > -1) res.push(item);
  }
  return res;
};

/**
 * @param {Array} arr1
 * @param {Array} arr2
 * @description 得到两个数组的并集, 两个数组的元素为数值或字符串
 */
export const getUnion = (arr1, arr2) => {
  return Array.from(new Set([...arr1, ...arr2]));
};

/**
 * @param {Array} target 目标数组
 * @param {Array} arr 需要查询的数组
 * @description 判断要查询的数组是否至少有一个元素包含在目标数组中
 */
export const hasOneOf = (targetarr, arr) => {
  return targetarr.some(_ => arr.indexOf(_) > -1);
};

/**
 * @param {String|Number} value 要验证的字符串或数值
 * @param {*} validList 用来验证的列表
 */
export function oneOf(value, validList) {
  for (let i = 0; i < validList.length; i++) {
    if (value === validList[i]) {
      return true;
    }
  }
  return false;
}

/**
 * @returns {String} 当前浏览器名称
 */
export const getExplorer = () => {
  const ua = window.navigator.userAgent;
  const isExplorer = exp => {
    return ua.indexOf(exp) > -1;
  };
  if (isExplorer("MSIE")) return "IE";
  else if (isExplorer("Firefox")) return "Firefox";
  else if (isExplorer("Chrome")) return "Chrome";
  else if (isExplorer("Opera")) return "Opera";
  else if (isExplorer("Safari")) return "Safari";
};

/**
 * 判断一个对象是否存在key，如果传入第二个参数key，则是判断这个obj对象是否存在key这个属性
 * 如果没有传入key这个参数，则判断obj对象是否有键值对
 */
export const hasKey = (obj, key) => {
  if (key) return key in obj;
  else {
    let keysArr = Object.keys(obj);
    return keysArr.length;
  }
};

/**
 * @param {*} obj1 对象
 * @param {*} obj2 对象
 * @description 判断两个对象是否相等，这两个对象的值只能是数字或字符串
 */
export const objEqual = (obj1, obj2) => {
  const keysArr1 = Object.keys(obj1);
  const keysArr2 = Object.keys(obj2);
  if (keysArr1.length !== keysArr2.length) {
    return false;
  } else if (keysArr1.length === 0 && keysArr2.length === 0) {
    return true;
  } else {
    /* eslint-disable-next-line */
    return !keysArr1.some(key => obj1[key] != obj2[key]);
  }
};

/**
 * @param {*} obj1 数据类型
 * @param {*} obj2 数据
 * @description 判断数据格式是否满足规定
 */
export const checkData = (obj1, obj2) => {
  // console.log(obj1, obj2, typeof obj2, _.isArray(obj2), _.isObject(obj2));
  if (obj1 !== undefined) {
    if (_.isArray(obj2)) {
      for (var i = 0; i < obj2.length; i++) {
        var obj2Item = obj2[i];
        if (_.isObject(obj2Item)) {
          obj2[i] = checkData(obj1, obj2Item);
        }
      }
    } else if (_.isObject(obj2)) {
      for (var key in obj2) {
        var obj2Item = obj2[key];
        if (_.isObject(obj2Item)) {
          obj2[key] = checkData(obj1, obj2Item);
        } else {
          if (obj1[key] !== undefined) {
            if (obj1[key] == "number") {
              if (obj2[key] != "" && obj2[key] != null) {
                obj2[key] = Number(obj2[key]);
              } else {
                obj2[key] = null;
              }
            } else {
              if (obj2[key] == "") {
                obj2[key] = null;
              }
            }
          } else {
            if (obj2[key] == "") {
              obj2[key] = null;
            }
          }
        }
      }
    }
  }
  var obj2End = [];
  if (_.isArray(obj2)) {
    obj2.map(obj2Item => {
      var flag = false;
      for (var key in obj2Item) {
        if (key != "steelCodes" && key != "md5") {
          if (_.isArray(obj2Item[key])) {
            if (obj2Item[key].length > 0) {
              flag = true;
            }
          } else {
            if (obj2Item[key] != null) {
              flag = true;
            }
          }
        }
      }
      if (flag) {
        obj2End.push(obj2Item);
      }
    });
  } else {
    obj2End = obj2;
  }
  return obj2End;
};
export const checkDataGuo = (obj1, obj2) => {
  // console.log(obj1, obj2, typeof obj2, _.isArray(obj2), _.isObject(obj2));
  if (obj1 !== undefined) {
    if (_.isArray(obj2)) {
      for (var i = 0; i < obj2.length; i++) {
        var obj2Item = obj2[i];
        if (_.isObject(obj2Item)) {
          obj2[i] = checkDataGuo(obj1, obj2Item);
        }
      }
    } else if (_.isObject(obj2)) {
      for (var key in obj2) {
        var obj2Item = obj2[key];
        if (_.isObject(obj2Item)) {
          obj2[key] = checkDataGuo(obj1, obj2Item);
        } else {
          if (obj1[key] !== undefined && obj1[key] == "number") {
            if (obj2[key] !== "") {
              obj2[key] = Number(obj2[key]);
            } else {
              obj2[key] = null;
            }
          }
        }
      }
    }
  }
  return obj2;
};

export const thkCompare = (propMin, propMax, data) => {
  var isErrThk = false;
  var indexAll = [];
  try {
    if (data != undefined && _.isArray(data)) {
      data.map((item, itemIndex) => {
        // 获取当前行的最小值和最大值
        var min = Number(item[propMin]);
        var max = Number(item[[propMax]]);
        // 检查是否为空
        if (item[propMin] == "" || item[propMax] == "") {
          isErrThk = true;
          indexAll.push(itemIndex);
        }
        // 检查最小值是否小于最大值
        if (min > max) {
          isErrThk = true;
          indexAll.push(itemIndex);
        }

        // 检查下一行的最小值是否大于当前行的最大值（如果不是最后一行）
        if (itemIndex < data.length - 1) {
          var nextMin = Number(data[itemIndex + 1][propMin]);
          if ((nextMin - max).toFixed(3) != 0.01) {
            console.error(
              "错误：" +
                (nextMin - max) +
                "上一行的上限跟下-行的下限之间差0.01， 在行 " +
                (itemIndex + 2) +
                " 中，厚度最小值 " +
                nextMin +
                " 不大于上一行的最大值 " +
                max
            );
            indexAll.push(itemIndex + 1);
            isErrThk = true;
          }
        }
      });
    }
  } catch (e) {
    console.error(e);
  }
  return {
    isErrThk: isErrThk,
    indexAll: indexAll
  };
};
